//package com.zhss.security.demospringsecurity.config;
//
//import com.zhss.security.demospringsecurity.service.MyUserDetailsService;
//import com.zhss.security.demospringsecurity.service.MyUserDetailsService2;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.annotation.Order;
//import org.springframework.security.config.annotation.ObjectPostProcessor;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
//import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//import org.springframework.security.web.util.matcher.OrRequestMatcher;
//
///**
// * @ClassName SecurityConfig
// * @Author lee ji/liang
// * @DateTime 2021/9/9 14:15
// * @Version 1.0
// */
//@Configuration
//@Order(2)
//public class SecurityConfig2 extends WebSecurityConfigurerAdapter {
//
////	@Autowired
////	private DataSource dataSource;
//	@Autowired
//	private MyUserDetailsService myUserDetailsService;
//	@Autowired
//	private MyUserDetailsService2 myUserDetailsService2;
//	@Override
//	protected void configure(HttpSecurity http) throws Exception {
//		http.antMatcher("/2/**")
//				.authorizeRequests()
//				.antMatchers("/vc.jpg").permitAll()
//				.anyRequest().authenticated()
//				.and()
//				.formLogin()
//
//				.loginPage("/mylogin.html")
//				.loginProcessingUrl("/2/doLogin")
//				.defaultSuccessUrl("/index")
//				.successHandler(new MyAuthenticationSuccessHandler())
////				.failureUrl("/error.html")//登陆失败后, 直接跳转到错误页面, 是客户端的重定向.不方便携带请求失败的异常信息, (url)
//				.failureForwardUrl("/mylogin.html")
////				.failureHandler(new MyAuthenticationFailureHandler())
//				// 通过后置处理器指定
////				.usernameParameter("uname")
////				.passwordParameter("passwd")
//				.permitAll()
//				// 自定义后置过滤器, 设置用户名和密码变量名称, 需要和表单中保持一致.
//				.withObjectPostProcessor(new ObjectPostProcessor<UsernamePasswordAuthenticationFilter>() {
//					@Override
//					public <O extends UsernamePasswordAuthenticationFilter> O postProcess(O o) {
//						o.setUsernameParameter("name");
//						o.setPasswordParameter("passwd");
//						o.setAuthenticationSuccessHandler(new MyAuthenticationSuccessHandler());
//						return o;
//					}
//				})
//				.and()
//				.logout()
////				.logoutUrl("/logout")
//				.logoutRequestMatcher(new OrRequestMatcher(
//						new AntPathRequestMatcher("/logout1", "GET"),
//						new AntPathRequestMatcher("/logout2", "GET")
//				))
//				.defaultLogoutSuccessHandlerFor(new MyLogoutSuccessHandler(), new AntPathRequestMatcher("/logout1", "GET"))
//				.defaultLogoutSuccessHandlerFor(new MyLogoutSuccessHandlerSecond(), new AntPathRequestMatcher("/logout2", "GET"))
//				.invalidateHttpSession(true)
//				.clearAuthentication(true)
////				.logoutSuccessUrl("/mylogin.html")
////				.logoutSuccessHandler(new MyLogoutSuccessHandler())
//				.and()
//				.csrf().disable()
//				.userDetailsService(myUserDetailsService2);
//	}
//
//
////	@Override
////	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
////		//基于内存
//////		InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
//////		manager.createUser(User.withUsername("leejiliang").password("{noop}123456").roles("admin","master").build());
//////		manager.createUser(User.withUsername("lizhixian").password("{noop}1234567").roles("admin","master").build());
//////		auth.userDetailsService(manager);
////
//////		JdbcUserDetailsManager manager = new JdbcUserDetailsManager(dataSource);
//////		if(manager.userExists("leejiliang")){
//////			manager.createUser(User.withUsername("leejiliang").password("{noop}123456").roles("admin","master").build());
//////		}
//////		if(manager.userExists("lizhixian")){
//////			manager.createUser(User.withUsername("lizhixian").password("{noop}1234567").roles("admin","master").build());
//////		}
//////		auth.userDetailsService(manager);
////
////		auth.userDetailsService(myUserDetailsService);
////	}
//
//
//	/**
//	 * 定义多个数据源
//	 * @return
//	 */
////	@Override
////	@Bean
////	public AuthenticationManager authenticationManagerBean() throws Exception {
////		DaoAuthenticationProvider daoAuthenticationProvider1 = new DaoAuthenticationProvider();
////		daoAuthenticationProvider1.setUserDetailsService(myUserDetailsService);
////
////		DaoAuthenticationProvider daoAuthenticationProvider2 = new DaoAuthenticationProvider();
////		daoAuthenticationProvider2.setUserDetailsService(myUserDetailsService2);
////
//////		ProviderManager providerManager = new ProviderManager(daoAuthenticationProvider1, daoAuthenticationProvider2);
////		ProviderManager providerManager = new ProviderManager(daoAuthenticationProvider2);
////		return providerManager;
////	}
//
////	@Bean
////	AuthenticationProvider kaptchaAuthenticationProvider() {
////		final KaptchaAuthenticationProvider kaptchaAuthenticationProvider = new KaptchaAuthenticationProvider();
////		kaptchaAuthenticationProvider.setUserDetailsService(myUserDetailsService);
////		return kaptchaAuthenticationProvider;
////	}
////	@Bean
////	AuthenticationProvider kaptchaAuthenticationProvider2() {
////		final KaptchaAuthenticationProvider kaptchaAuthenticationProvider = new KaptchaAuthenticationProvider();
////		kaptchaAuthenticationProvider.setUserDetailsService(myUserDetailsService2);
////		return kaptchaAuthenticationProvider;
////	}
//
//	/**
//	 * 添加验证码逻辑
//	 *
//	 * @return
//	 * @throws Exception
//	 */
////	@Override
////	protected AuthenticationManager authenticationManager() throws Exception {
//////		DaoAuthenticationProvider daoAuthenticationProvider1 = new DaoAuthenticationProvider();
//////		daoAuthenticationProvider1.setUserDetailsService(myUserDetailsService);
//////
//////		DaoAuthenticationProvider daoAuthenticationProvider2 = new DaoAuthenticationProvider();
//////		daoAuthenticationProvider2.setUserDetailsService(myUserDetailsService2);
////
//////		ProviderManager providerManager = new ProviderManager(daoAuthenticationProvider1, daoAuthenticationProvider2);
//////		ProviderManager providerManager = new ProviderManager(daoAuthenticationProvider2);
////		ProviderManager providerManager = new ProviderManager(kaptchaAuthenticationProvider(), kaptchaAuthenticationProvider2());
////		return providerManager;
////	}
//
//	SavedRequestAwareAuthenticationSuccessHandler successHandler() {
//		SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
//		handler.setDefaultTargetUrl("/index");
//		handler.setTargetUrlParameter("target_test");
//		return handler;
//	}
//
//	SimpleUrlAuthenticationFailureHandler failureHandler() {
//		SimpleUrlAuthenticationFailureHandler failureHandler =
//				new SimpleUrlAuthenticationFailureHandler("/mylogin.html");
//		failureHandler.setUseForward(true);
//		return failureHandler;
//	}
//}
